
package com.doc2video.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doc2video.common.Result;
import com.doc2video.dto.DocumentUploadDTO;
import com.doc2video.entity.Document;
import com.doc2video.service.DocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 文档Controller
 */
@Api(tags = "文档管理")
@RestController
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @ApiOperation("上传文档")
    @PostMapping("/upload")
    public Result<Document> uploadDocument(@RequestParam("file") MultipartFile file,
                                          @RequestParam(value = "description", required = false) String description,
                                          HttpServletRequest request) {
        DocumentUploadDTO uploadDTO = new DocumentUploadDTO();
        uploadDTO.setFile(file);
        uploadDTO.setDescription(description);
        return documentService.uploadDocument(uploadDTO, request);
    }

    @ApiOperation("分页查询用户文档列表")
    @GetMapping("/list")
    public Result<Page<Document>> getUserDocuments(@RequestParam(defaultValue = "1") Integer current,
                                                 @RequestParam(defaultValue = "10") Integer size,
                                                 HttpServletRequest request) {
        Page<Document> page = new Page<>(current, size);
        return documentService.getUserDocuments(page, request);
    }

    @ApiOperation("根据ID查询文档")
    @GetMapping("/{id}")
    public Result<Document> getDocumentById(@PathVariable Long id, HttpServletRequest request) {
        return documentService.getDocumentById(id, request);
    }

    @ApiOperation("删除文档")
    @DeleteMapping("/{id}")
    public Result<String> deleteDocument(@PathVariable Long id, HttpServletRequest request) {
        return documentService.deleteDocument(id, request);
    }

    @ApiOperation("提取文档内容")
    @PostMapping("/{id}/extract")
    public Result<String> extractDocumentContent(@PathVariable Long id, HttpServletRequest request) {
        return documentService.extractDocumentContent(id);
    }

    @ApiOperation("下载文档")
    @GetMapping("/{id}/download")
    public Result<byte[]> downloadDocument(@PathVariable Long id, HttpServletRequest request) {
        return documentService.downloadDocument(id, request);
    }
}
